module.service('Property', ['$rootScope', function ($rootScope) {
    var service = {
        _baseUrl: "/azeroth/back/property/",

        currentProperty: {},
        propertyList: [],
        propertyValueList: [],
        totalSceneCount: 0,
        propertyValueCount: 0,
        queryList: function (title, propertyType, currentPage, pageSize, callback) {

            currentPage = currentPage ? currentPage : 1;
            var paramData = {
                pageSize: pageSize,
                firstRow: (currentPage - 1) * pageSize,
                title: title
            };

            if (propertyType && propertyType != 0) {
                paramData['propertyType'] = propertyType;
            }

            $rootScope.ajaxRequestPost(service._baseUrl + "getPropertyList.htm", paramData, function (resultJson) {
                angular.copy(resultJson.data, service.propertyList);
                service.totalSceneCount = resultJson.totalCount;
                if (callback) {
                    callback();
                }
            });

        },
        insert: function (title, editType, propertyType, couldAppendImage, callback) {
            var paramData = {
                title: title,
                editType: 1,
                propertyType: propertyType,
                couldAppendImage: couldAppendImage
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "publishProperty.htm", paramData, function (resultJson) {
                if (callback) {
                    callback();
                }
            });
        },
        delete: function (id, callback) {
            var paramData = {
                id: id
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "deleteProperty.htm", paramData, function (resultJson) {
                //根据id删除掉该数据
                var removeIndex;
                $.each(service.propertyList, function (index, result) {
                    if (result['id'] == id) {
                        removeIndex = index;
                    }
                });
                service.propertyList.splice(removeIndex, 1);
                if (callback) {
                    callback();
                }
            });
        },
        insertPropertyValue: function (propertyMetaId, propertyMetaTitle, couldAppendImage, value, callback) {
            var paramData = {
                propertyMetaId: propertyMetaId,
                propertyMetaTitle: propertyMetaTitle,
                couldAppendImage: couldAppendImage,
                value: value
            };
            $rootScope.ajaxRequestPost("/azeroth/back/propertyvalue/publishPropertyValue.htm", paramData, function (resultJson) {

                service.propertyValueList.push({
                    id: resultJson.data,
                    gmtCreate: new Date(),
                    gmtModified: new Date(),
                    propertyMetaId: propertyMetaId,
                    value: value,
                    feature: ""
                });
                if (callback) {
                    callback();
                }
            });
        },
        deletePropertyValue: function (id, callback) {
            var paramData = {
                id: id
            };
            $rootScope.ajaxRequestPost("/azeroth/back/propertyvalue/deletePropertyValue.htm", paramData, function (resultJson) {

                //根据id删除掉该数据
                var removeIndex;
                $.each(service.propertyValueList, function (index, result) {
                    if (result['id'] == id) {
                        removeIndex = index;
                    }
                });
                service.propertyValueList.splice(removeIndex, 1);

                if (callback) {
                    callback();
                }
            });
        },
        getPropertyValues: function (propertyId, currentPage, pageSize, callback) {
            var paramData = {
                propertyId: propertyId,
                page: currentPage,
                pageSize: pageSize
            };
            $rootScope.ajaxRequestPost("/azeroth/back/propertyvalue/getPropertyValueList.htm", paramData, function (resultJson) {

                angular.copy(resultJson.data, service.propertyValueList);
                service.propertyValueCount = resultJson.totalCount;

                if (callback) {
                    callback();
                }
            });
        }
    };
    return service;
}]);
